package org.kfive.nova.test.gc;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 16-6-4
 * Time: 下午6:48
 * To change this template use File | Settings | File Templates.
 */
public class FinalizeEscapeGC {

    public static FinalizeEscapeGC SAVE_HOOK = null;

    public void isAlive(){
        if(SAVE_HOOK!=null){
            System.out.println("yes,i am still alive;");
        }else {
            System.out.println("no, i am dead");
        }
    }

    @Override
    public void finalize() throws Throwable{
        super.finalize();
        System.out.println("finalize method executed");
        FinalizeEscapeGC.SAVE_HOOK = this;
    }

    public static void main(String[] args) throws InterruptedException {
        SAVE_HOOK = new FinalizeEscapeGC();

        SAVE_HOOK = null;
        System.gc();
        Thread.sleep(500);
        SAVE_HOOK.isAlive();

        SAVE_HOOK = null;
        System.gc();
        Thread.sleep(500);
        if(SAVE_HOOK==null){
            System.out.println("no, i am dead");
        }else {
            System.out.println("yes,i am still alive");
        }

    }
}
